﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Collections;
using DddCms.Presentation.UIComponent.Form;
using DddCms.Models.UIComponent.Window;

namespace DddCms.Models.UIComponent.Form
{
    /// <summary>
    /// Aggregate form
    /// </summary>
    public class FormViewModel : IEnumerable
    {
        public Func<MvcForm> MvcForm { get; set; }
        public Dictionary<int, FormRowViewModel> RowsFields { get; set; }
        public Dictionary<int, FormMessageViewModel> RowsMessages { get; set; }
        public string Width { get; set; }
        public string Title { get; set; }
        public string Error { get; set; }
        public WindowPositionViewModel Position { get; set; }
        public int ColSpan(int fieldCount)
        {
            return this.RowsFields.Max(f => f.Value.Fields.Count) - fieldCount + 1;
        }
        private int currentIndex;

        public FormViewModel()
        {
            currentIndex = 0;
            RowsFields = new Dictionary<int, FormRowViewModel>();
            RowsMessages = new Dictionary<int, FormMessageViewModel>();
            Error = null;
            Height = 0;
            Position = WindowPositionViewModel.FullSize;
        }
        public int Height { get; set; }

        public void AddRow(FormRowViewModel row)
        {
            // Si c'est une première ligne
            if (RowsFields.Count == 0)
            {
                row.IsTheNewOfTableField = true;
            }
            // Si la précedent ligne étais un message
            if (RowsMessages.ContainsKey(currentIndex - 1))
            {
                row.IsTheNewOfTableField = true;
            }

            // La dernier ajouter ce retrouve en bas, on le signale par un flag
            row.IsTheLastTableField = true;
            
            // On enleve ce flag au précedent si il y'en a un 
            if (RowsFields.Count > 0)
            {
                RowsFields.Last().Value.IsTheLastTableField = false;
            }
            RowsFields.Add(currentIndex, row);
            currentIndex++;
        }
        public void AddMessage(string message)
        {
            FormMessageViewModel rowMessage = new FormMessageViewModel();
            rowMessage.Message = message;

            // Si la précedent ligne étais une ligne de champ
            if (RowsFields.ContainsKey(currentIndex - 1))
            {
                rowMessage.IsAfterARowField = true;
            }
            RowsMessages.Add(currentIndex, rowMessage);
            currentIndex++;
        }
        public int getIndex()
        {
            return currentIndex;
        }

        public IEnumerator GetEnumerator()
        {
            List<object> rep = new List<object>();

            // Fusion de la liste des lignes messages et des lignes formulaire
            for (int i = 0; i < currentIndex; i++)
            {
                if (RowsMessages.ContainsKey(i))
                    rep.Add(RowsMessages[i]);
                else if (RowsFields.ContainsKey(i))
                    rep.Add(RowsFields[i]);
            }
            return rep.GetEnumerator();
        }
    }
}